package com.example.monitor.service.impl;

import com.example.monitor.model.DO.Camera;
import com.example.monitor.model.VO.CameraVO;
import com.example.monitor.model.VO.ResultVO;
import com.example.monitor.repository.CameraRepository;
import com.example.monitor.service.CameraService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CameraServiceImpl implements CameraService {

    private final CameraRepository cameraRepository;

    @Override
    public ResultVO<List<CameraVO>> getAllCameras() {
        List<Camera> cameras = cameraRepository.findAllByOrderByCameraIdAsc();

        List<CameraVO> cameraVOs = cameras.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());

        return ResultVO.success("摄像头列表查询成功", cameraVOs);
    }

    @Override
    public ResultVO<CameraVO> addCamera(CameraVO cameraVO) {
        Camera camera = new Camera();
        camera.setName(cameraVO.getCameraName());
        camera.setLocation(cameraVO.getLocation());
        camera.setStatus(cameraVO.getStatus());
        camera.setImageUrl(cameraVO.getImageUrl());
        camera.setWorkshop(cameraVO.getWorkshop());

        Camera savedCamera = cameraRepository.save(camera);
        CameraVO resultVO = convertToVO(savedCamera);

        return ResultVO.success("摄像头添加成功", resultVO);
    }

    @Override
    public ResultVO<CameraVO> updateCamera(Long cameraId, CameraVO cameraVO) {
        // 检查摄像头是否存在
        Camera existingCamera = cameraRepository.findById(cameraId)
                .orElse(null);

        if (existingCamera == null) {
            ResultVO<CameraVO> result = new ResultVO<>();
            result.setCode(404); // 或其他错误码
            result.setMessage("摄像头不存在");
            result.setData(null);
            return result;
        }

        // 更新摄像头信息
        existingCamera.setName(cameraVO.getCameraName());
        existingCamera.setLocation(cameraVO.getLocation());
        existingCamera.setStatus(cameraVO.getStatus());
        existingCamera.setImageUrl(cameraVO.getImageUrl());
        existingCamera.setWorkshop(cameraVO.getWorkshop());

        Camera updatedCamera = cameraRepository.save(existingCamera);
        CameraVO resultVO = convertToVO(updatedCamera);

        return ResultVO.success("摄像头信息更新成功", resultVO);
    }

    @Override
    public ResultVO<Void> deleteCamera(Long cameraId) {
        // 检查摄像头是否存在
        if (!cameraRepository.existsById(cameraId)) {
            return ResultVO.success("摄像头不存在");
        }

        cameraRepository.deleteById(cameraId);
        return ResultVO.success("摄像头删除成功");
    }

    private CameraVO convertToVO(Camera camera) {
        CameraVO vo = new CameraVO();
        vo.setCameraName(camera.getName());
        vo.setLocation(camera.getLocation());
        vo.setStatus(camera.getStatus());
        vo.setWorkshop(camera.getWorkshop());

        // 使用数据库中的image_url，如果为空则使用默认图片
        String imageUrl = camera.getImageUrl() != null ?
                camera.getImageUrl() :
                "/static/images/camera_default.jpg";
        vo.setImageUrl(imageUrl);

        return vo;
    }

    // 用于将CameraVO转换为Camera实体的辅助方法
    private Camera convertToDO(CameraVO cameraVO) {
        Camera camera = new Camera();
        camera.setName(cameraVO.getCameraName());
        camera.setLocation(cameraVO.getLocation());
        camera.setStatus(cameraVO.getStatus());
        camera.setImageUrl(cameraVO.getImageUrl());
        camera.setWorkshop(cameraVO.getWorkshop());
        return camera;
    }
}